/*
 * FormWizard.java
 *
 * Created on May 13, 2006, 6:56 PM
 */

/**
 *
 * @author  Timoz
 */
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.io.*;

public class FormWizard extends javax.swing.JFrame {
    String selectedTable;  //String to store the user's selected Table
    java.util.Vector selectedFields;  //Vector to store the values of the selected Table Fields
    String formTitle;  //String to store the Form Title
    String selectedLayout;  //String to store the user selected Layout (Columnar By Default)
    boolean modify=true;  //Boolean to store if the user wants to modify the design of the Form in the end or open it in Output View (Modify by Default)
    int xLabel,yLabel;  //Integers to set the labels positions
    int xText,yText;  //Integers to set the Textfields positions
    int lastFormIndex;  //Integer to store the last number of Form Name that's created by default if the user doesn't enter a Form name
    int xPanel,yPanel;  //Integers to set the size of the Design Panel
    boolean flagAutoForm=false;  //boolean to check if the user has created a Form from AutoForm Wizard
    /** Creates new form FormWizard */
    public FormWizard(Vector myForms) {
        Vector availableTables=MainWindow.dbCon.getTables();
        if (availableTables.isEmpty()==false) {
            initComponents();
            //Initialize Variables and the Form
            int sizeForms = myForms.size();
            String str;
            for (int i=0;i<sizeForms;i++) {
                str=(String)myForms.get(i);
                if (str.length()>4) {
                    if (str.substring(0,4).equals("Form")) {
                        int lastIndex = Integer.parseInt(str.substring(5));
                        if (lastIndex > lastFormIndex) {
                            lastFormIndex=lastIndex;
                        }
                    }
                }
            }
            lastFormIndex++;
            formTitle="Form "+lastFormIndex;
            
            selectedFields = new java.util.Vector();
            selectedLayout= new String("Columnar");
            FormFields formFields = new FormFields(this);
            setLayout(new FlowLayout());
            setResizable(false);
            setAlwaysOnTop(true);
            setLocation(200,100);
            setTitle("Form Wizard");
            add(formFields);
            setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null,"The selected Database doesn't contain any tables To create a Form from","Error",JOptionPane.ERROR_MESSAGE);
            dispose();
        }
    }
    
    public FormWizard(Vector myForms, String selectedTable) {
        Vector availableTables=MainWindow.dbCon.getTables();
        if (availableTables.isEmpty()==false) {
        this.selectedTable=selectedTable;
        initComponents();
        //Initialize Variables and the Form
        int sizeForms = myForms.size();
        String str;
        for (int i=0;i<sizeForms;i++) {
            str=(String)myForms.get(i);
            if (str.length()>4) {
                if (str.substring(0,4).equals("Form")) {
                    int lastIndex = Integer.parseInt(str.substring(5));
                    if (lastIndex > lastFormIndex) {
                        lastFormIndex=lastIndex;
                    }
                }
            }
        }
        lastFormIndex++;
        formTitle="Form "+lastFormIndex;
        
        selectedFields = new java.util.Vector();
        selectedLayout= new String("Columnar");
        FormFields formFields = new FormFields(this);
        setLayout(new FlowLayout());
        setResizable(false);
        setAlwaysOnTop(true);
        setLocation(200,100);
        setTitle("Form Wizard");
        add(formFields);
        setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null,"The selected Database doesn't contain any tables To create a Form from","Error",JOptionPane.ERROR_MESSAGE);
            dispose();
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 460, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 369, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * A Method that analyzes the user selected Form Parameters (Table Fields, Layout, etc)
     * through the Form Wizard and then creates a new Form accordingly.
     */
    public void createForm() {
        String curDir = System.getProperty("user.dir");
        boolean flagNameExists=false;
        for (String str : MainWindow.myForms) {
            if (formTitle.equals(str)) {
                flagNameExists=true;
            }
        }
        if (flagNameExists) {
            JOptionPane.showMessageDialog(this,"A Form With The Name You Specified Already Exists!\nPlease Choose Another Name");
        } else {
            if (modify==true) {  //if the user has selected to modify the Form's Design
                
                if (selectedLayout.equals("Columnar")) {  //if the user has selected the Columnar Layout
                    MyFrame myFrame=createColumnarForm();
                    myFrame.setVisible(true);
                }
                
                //If the selected Layout is Justified or Tabular
                else if  (selectedLayout.equals("Tabular")) {
                    MyFrame myFrame=createTabularForm();
                    myFrame.setVisible(true);
                }
                //Check if the selected Layout is Justified
                else if  (selectedLayout.equals("Justified")) {
                    MyFrame myFrame=createJustifiedForm();
                    myFrame.setVisible(true);
                }
            }
            else  //if the user has chosen to open the Form in View Mode directly
            {
                MyFrame myFrame=null;
                if (selectedLayout.equals("Columnar")) {  //if the user has selected the Columnar Layout
                    myFrame=createColumnarForm();
                }
                
                //If the selected Layout is Justified or Tabular
                else if  (selectedLayout.equals("Tabular")) {
                    myFrame=createTabularForm();
                }
                //Check if the selected Layout is Justified
                else if  (selectedLayout.equals("Justified")) {
                    myFrame=createJustifiedForm();
                }
            OutputPanel outputPanel = new OutputPanel (MainWindow.dbCon,myFrame.componentList, myFrame.panel.getBackground(), myFrame.panel.getSize(), MainWindow.recordsLimit);
            MainPanel mainPanel = new MainPanel (outputPanel.compPanel);
            outputPanel.compPanel.setLocation(0,0);
            mainPanel.drawPanel.add(outputPanel.compPanel);
            mainPanel.insertRecordNavigationMenu (outputPanel);
            OutputFrame outputFrame = new OutputFrame (mainPanel);
            outputFrame.refreshScrolls();
            outputFrame.setTitle(formTitle);
            outputFrame.setVisible(true);
            }
        }
    }
    
    private MyFrame createColumnarForm() {
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension d=kit.getScreenSize();
        MyFrame myFrame = new MyFrame(d);  //Create A new design panel
        Vector <MyTextFieldContainer> text = new Vector <MyTextFieldContainer>();  //Vector to store the textfields
        Vector <MyLabelContainer> labels = new Vector <MyLabelContainer>();  //Vector to store the labels
        //Note: For each selected Table Field, a Textfield and a Label will be created.
        
        //Initialize the Positions
        xLabel=10;
        yLabel=10;
        xText=180;
        yText=10;
        xPanel=360;
        yPanel=0;
        
        int screenHeight=(int)(d.getHeight()*0.85);
        
        int yPanel2=0;
        boolean columns=false;
        
        for (int i=0;i<selectedFields.size();i++) {
            if (yPanel<screenHeight-55) {
                yPanel+=50;
            } else {
                yPanel2=yPanel;
                columns=true;
                yPanel=0;
                xPanel+=400;
            }
        }
        if (columns) {
            yPanel=yPanel2;
        }
        myFrame.panel.setSize(xPanel,yPanel);
        
        //Create A Textfield and a Label for each selected Field
        for (int i=0;i<selectedFields.size();i++) {
            String str=(String)selectedFields.get(i);
            MyTextFieldContainer textField;
            MyLabelContainer label;
            if (flagAutoForm) {
                textField = new MyTextFieldContainer(selectedTable+"."+str,myFrame);
                label = new MyLabelContainer(str,myFrame);
            } else {
                textField = new MyTextFieldContainer(str,myFrame);
                label = new MyLabelContainer(str.substring(str.indexOf('.')+1),myFrame);
            }
            ((JLabel)label.getComponent()).setSize(150,30);
            ((JLabel)label.getComponent()).setLocation(xLabel,yLabel);
            ((JTextField)textField.getComponent()).setSize(150,30);
            ((JTextField)textField.getComponent()).setLocation(xText,yText);
            if (flagAutoForm) {
                ((JTextField)textField.getComponent()).setName(str+"_Text");  //set the names of the Textfields
                ((JLabel)label.getComponent()).setName(str+"_Label");
            } else {
                ((JTextField)textField.getComponent()).setName(str.substring(str.indexOf('.')+1)+"_Text");  //set the names of the Textfields
                ((JLabel)label.getComponent()).setName(str.substring(str.indexOf('.')+1)+"_Label");
            }
            
            //Increment the Y Positions
            yLabel+=50;
            yText+=50;
            
            //Check if the Y Position has reached the panel's Height then
            //start at the top again but increment X Position (Simulating a new column)
            if ((yText+30)>myFrame.panel.getHeight()) {
                yLabel=10;
                yText=10;
                xLabel+=400;
                xText+=400;
            }
            labels.add(label);
            text.add(textField);
        }
        createForm(myFrame,text,labels);
        return myFrame;
    }
    
    private MyFrame createTabularForm() {
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension d=kit.getScreenSize();
        MyFrame myFrame = new MyFrame(d);  //Create A new design panel
        Vector <MyTextFieldContainer> text = new Vector <MyTextFieldContainer>();  //Vector to store the textfields
        Vector <MyLabelContainer> labels = new Vector <MyLabelContainer>();  //Vector to store the labels
        //Note: For each selected Table Field, a Textfield and a Label will be created.
        xLabel=10;
        yLabel=10;
        xText=10;
        yText=50;
        xPanel=10;
        yPanel=90;
        
        for (int i=0;i<selectedFields.size();i++) {
            xPanel+=155;
        }
        myFrame.panel.setSize(xPanel,yPanel);
        
        for (int i=0;i<selectedFields.size();i++) {
            String str=(String)selectedFields.get(i);
            MyTextFieldContainer textField;
            MyLabelContainer label;
            if (flagAutoForm) {  //if the user has created the Form from AutoWizard
                //set the textfield name to Tablename.Fieldname
                //Note: this was done automatically by the Wizard (FormFields Class)
                //but since the user doesn't pass through this Class in AutoForm the flagAutoForm was necessary
                textField = new MyTextFieldContainer(selectedTable+"."+str,myFrame);
                label = new MyLabelContainer(str,myFrame);
            } else {
                textField = new MyTextFieldContainer(str,myFrame);
                label = new MyLabelContainer(str.substring(str.indexOf('.')+1),myFrame);
            }
            ((JLabel)label.getComponent()).setSize(150,30);
            ((JLabel)label.getComponent()).setLocation(xLabel,yLabel);
            ((JTextField)textField.getComponent()).setSize(150,30);
            ((JTextField)textField.getComponent()).setLocation(xText,yText);
            if (flagAutoForm) {
                ((JTextField)textField.getComponent()).setName(str+"_Text");  //set the names of the Textfields
                ((JLabel)label.getComponent()).setName(str+"_Label");
            } else {
                ((JTextField)textField.getComponent()).setName(str.substring(str.indexOf('.')+1)+"_Text");  //set the names of the Textfields
                ((JLabel)label.getComponent()).setName(str.substring(str.indexOf('.')+1)+"_Label");
            }
            ((JLabel)label.getComponent()).setHorizontalAlignment(JLabel.CENTER);
            xLabel+=155;
            xText+=155;
            labels.add(label);
            text.add(textField);
        }
        createForm(myFrame,text,labels);
        return myFrame;
    }
    
    private MyFrame createJustifiedForm() {
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension d=kit.getScreenSize();
        MyFrame myFrame = new MyFrame(d);  //Create A new design panel
        Vector <MyTextFieldContainer> text = new Vector <MyTextFieldContainer>();  //Vector to store the textfields
        Vector <MyLabelContainer> labels = new Vector <MyLabelContainer>();  //Vector to store the labels
        //Note: For each selected Table Field, a Textfield and a Label will be created.
        xLabel=10;
        yLabel=10;
        xText=10;
        yText=40;
        xPanel=10;
        yPanel=90;
        
        int screenWidth=(int)(d.getWidth()*0.85);
        int xPanel2=10;
        boolean rows=false;  //boolean to check if the Form will require space greater than the available screen size
        
        for (int i=0;i<selectedFields.size();i++) {
            if (xPanel<screenWidth-165) {
                xPanel+=160;
            } else {
                xPanel2=xPanel;
                rows=true;
                yPanel+=90;
                xPanel=10;
            }
        }
        if (rows) {
            xPanel=xPanel2;
        }
        myFrame.panel.setSize(xPanel,yPanel);
        
        for (int i=0;i<selectedFields.size();i++) {
            String str=(String)selectedFields.get(i);
            MyTextFieldContainer textField;
            MyLabelContainer label;
            if (flagAutoForm) {
                textField = new MyTextFieldContainer(selectedTable+"."+str,myFrame);
                label = new MyLabelContainer(str,myFrame);
            } else {
                textField = new MyTextFieldContainer(str,myFrame);
                label = new MyLabelContainer(str.substring(str.indexOf('.')+1),myFrame);
            }
            ((JLabel)label.getComponent()).setSize(150,30);
            ((JLabel)label.getComponent()).setLocation(xLabel,yLabel);
            ((JTextField)textField.getComponent()).setSize(150,30);
            ((JTextField)textField.getComponent()).setLocation(xText,yText);
            if (flagAutoForm) {
                ((JTextField)textField.getComponent()).setName(str+"_Text");  //set the names of the Textfields
                ((JLabel)label.getComponent()).setName(str+"_Label");
            } else {
                ((JTextField)textField.getComponent()).setName(str.substring(str.indexOf('.')+1)+"_Text");  //set the names of the Textfields
                ((JLabel)label.getComponent()).setName(str.substring(str.indexOf('.')+1)+"_Label");
            }
            ((JLabel)label.getComponent()).setHorizontalAlignment(JLabel.CENTER);
            xLabel+=155;
            xText+=155;
            
            if ((xText+155)>myFrame.panel.getWidth()) {
                yLabel+=70;
                yText+=70;
                xLabel=10;
                xText=10;
            }
            labels.add(label);
            text.add(textField);
        }
        createForm(myFrame,text,labels);
        return myFrame;
    }
    
    private void createForm(MyFrame myFrame,Vector<MyTextFieldContainer> text, Vector <MyLabelContainer> labels) {
        //Add the Textfields and labels to the Component List and the Panel
        for (int i=0;i<text.size();i++) {
            myFrame.panel.add(text.get(i).getComponent());
            myFrame.panel.add(labels.get(i).getComponent());
            myFrame.componentList.add(text.get(i));
            myFrame.componentList.add(labels.get(i));
        }
        
        myFrame.setTitle(formTitle);
        myFrame.refreshScrolls();
        Save.save(formTitle,myFrame.panel,myFrame.componentList,selectedTable);
        myFrame.formatToolBar.refreshObjects();
        myFrame.formatToolBar.disableButtons();
        myFrame.setTableName(selectedTable);
        MainWindow.refreshFormList(formTitle);
        dispose();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    
}
